<?php
/**
 * Created by PhpStorm.
 * User: 曹乐清
 * Date: 2017/3/22
 * Time: 15:09
 */

namespace Api\Controller;


use Think\Controller;

class UserController extends Controller
{
    public function login(){
        $appid = "wxdea5bea284c15057";
        $secret = "159326a1e121edb999f199d076a785e5";
        $data=json_decode(file_get_contents('php://input'),true);
        $code=$data['code'];
        $get_token_url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$appid.'&secret='.$secret.'&code='.$code.'&grant_type=authorization_code';
        $res=cUrl($get_token_url);
        $openId=$res['openid'];
        $access_token=$res['access_token'];
        $info=D('client_user')->where(array('openid'=>$openId))->find();
        if(!$info){
            $data=array(
                'openid'=>$openId,
                'ac_time'=>time(),
                'access_token'=>$access_token
            );
            $id=D('client_user')->add($data);
        }else{
            $id=$info['id'];
        }
        $this->ajaxReturn(array('meta'=>array('code'=>0,"message"=>"登陆成功"),'data'=>array('token'=>$access_token,'user_id'=>$id)));
    }

    //获取轮播图
    public function getBanner(){
        $banner=D('banner')->select();
        foreach($banner as $key=>$value){
            $banner[$key]['thumb_url']='https://'.$_SERVER['HTTP_HOST'].'/Uploads/Cms/Banner/'.$value['thumb'];
            $banner[$key]['pic_url']='https://'.$_SERVER['HTTP_HOST'].'/Uploads/Cms/Banner/'.$value['pic'];
        }
        $this->ajaxReturn(array('item'=>$banner));
    }

    //获取展示列表
    public function getShow(){
        $id=$_GET['id'];
        $list=D('showpic')->where(array('pid'=>$id))->group('timestamp')->select();
        foreach($list as $key=>$value){
            $list[$key]['thumb']='https://'.$_SERVER['HTTP_HOST'].'/Uploads/Cms/showPic/'.$value['thumb'];
        }
        $this->ajaxReturn(array('list'=>$list,'count'=>count($list)));
    }

    //获取展示组详情
    public function getShowDetail(){
        $data=json_decode(file_get_contents("php://input"),true);
        $list=D('showpic')->where(array('pid'=>$data['pid'],'timestamp'=>$data['timestamp']))->select();
        foreach($list as $key=>$value){
            $list[$key]['thumb']='https://'.$_SERVER['HTTP_HOST'].'/Uploads/Cms/showPic/'.$value['thumb'];
            $list[$key]['pic']='https://'.$_SERVER['HTTP_HOST'].'/Uploads/Cms/showPic/'.$value['pic'];
        }
       echo json_encode(array('meta'=>array('code'=>0,'message'=>'调用成功'),'result'=>$list));
    }

    //添加收货地址
    public function addRess(){
        $data=json_decode(file_get_contents("php://input"),true);
        $add_array=array(
            'name'=>$data['name'],
            'phone'=>$data['phone'],
            'address'=>$data['address'],
            'cityID'=>$data['cityID'],
            'user_id'=>$data['user_id'],
            'ac_time'=>time()
        );
        $info=D('client_user_address')->where(array('user_id'=>$data['user_id']))->select();
        if(!$info){
            $add_array['is_code']=1;
        }

        $id=D('client_user_address')->add($add_array);
        if(!$id){
            $this->ajaxReturn(array('meat'=>array('code'=>false,'messgae'=>'新增失败')));
        }
        $this->ajaxReturn(array('meat'=>array('code'=>0,'message'=>'新增成功')));
    }

    //@todo  获取收货地址列表
    public  function getUserResslist(){
        $data=json_decode(file_get_contents("php://input"),true);
        $user_id=$data['user_id'];
        $list=D('client_user_address')->where(array('user_id'=>$user_id))->select();
        $this->ajaxReturn(array('meat'=>array('code'=>0,'message'=>'调用成功'),'data'=>$list));
    }
    //@todo 修改默认地址
    public function editUserRessCode(){
        $data=json_decode(file_get_contents("php://input"),true);
        $id=$data['id'];
        $user_id=$data['user_id'];
        $edit=D('client_user_address')->where(array('user_id'=>$user_id))->save(array('is_code'=>''));
        if($edit){
            $is_code=D('client_user_address')->save(array('id'=>$id,'is_code'=>1));
            if(is_numeric($is_code)){
                $list=D('client_user_address')->where(array('user_id'=>$user_id))->select();
                $this->ajaxReturn(array('meat'=>array('code'=>0,'message'=>'调用成功'),'data'=>$list));
            }
            $this->ajaxReturn(array('meat'=>array('code'=>40001,'message'=>'调用成功,操作失败')));
        }
        $this->ajaxReturn(array('meat'=>array('code'=>40001,'message'=>'调用成功,操作失败')));
    }

    //@todo 查询一条数据
    public function getUserAddRessInfo(){
        $data=json_decode(file_get_contents("php://input"),true);
        $id=$data['id'];
        $info=D('client_user_address')->find($id);
        $this->ajaxReturn(array('meat'=>array('code'=>0,'message'=>'调用成功'),'data'=>$info));
    }

    //@todo 修改地址
    public function editUserRess(){
        $data=json_decode(file_get_contents("php://input"),true);
        $edit=D('client_user_address')->save($data);
        if(is_numeric($edit)){
            $this->ajaxReturn(array('meat'=>array('code'=>0,'message'=>'调用成功')));
        }
        $this->ajaxReturn(array('meat'=>array('code'=>40002,'message'=>'调用成功,执行失败')));
    }
    //@todo 删除地址
    public function deletUserAddRessInfo(){
        $data=json_decode(file_get_contents("php://input"),true);
        $id=$data['id'];
        $del=D('client_user_address')->delete($id);
        if(!$del){
            $this->ajaxReturn(array('meat'=>array('code'=>40002,'message'=>'调用成功,执行失败')));
        }
        $this->ajaxReturn(array('meat'=>array('code'=>0,'message'=>'调用成功')));
    }

    //@todo  获取默认地址
    public function getSelectAddRessInfo(){
        $data=json_decode(file_get_contents("php://input"),true);
        $user_id=$data['user_id'];
        $info=D('client_user_address')->where(array('user_id'=>$user_id,'is_code'=>'1'))->find();
        if(!$info){
            $this->ajaxReturn(array('meat'=>array('code'=>40002,'message'=>'未找到指定数据')));
        }
        $this->ajaxReturn(array('meat'=>array('code'=>0,'message'=>'调用成功'),'data'=>$info));
    }

    //@todo 获取产品规格信息
    public function getProductList(){
        $data=json_decode(file_get_contents("php://input"),true);
        $type=$data['id'];
        $box=D('photo_box')->where(array('type'=>$type))->order('price desc')->select();
        foreach($box as $key=>$value){
            if($value['type'] == '1'){
                $box[$key]['type']='相框';
            }else{
                $box[$key]['type']='相册';
            }
        }
        $this->ajaxReturn(array('meat'=>array('code'=>0,'message'=>'调用成功'),'data'=>$box));
    }

}